﻿using System;
using System.ServiceModel;
using Library.Models;
using Library.Repository;
using Library.Utils;
using log4net;

namespace Library.Wcf
{

    public class UserWcfService : IUserWcfService
    {
        private static readonly ILog Log = LogManager.GetLogger("UserWcfService");

        public UserWcfService()
        {
            Log.Info("constructor");
            userRepository = ServiceLocator.Instance.GetService<IUserRepository>();
            Log.Info("constructor_end");
        }

        private IUserRepository userRepository;

        public User ValidateUser(string login, string pass)
        {
            Log.Info("validate: "+ login);
            try
            {
                User user = userRepository.GetOrDie(login);
                return user.Pass == pass ? user : null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public User AddUser(string login, string pass, User newUser)
        {
            Log.Info("AddUser: " + newUser.Login);
            User currentUser = userRepository.GetOrDie(login);

            if (currentUser.Pass == pass && currentUser.Role == (int)UserRole.Admin)
                return userRepository.Add(newUser);
            else return null;
        }

        public void ChangePassword(string login, string pass, string newPass)
        {
            Log.Info("ChangePassword: " + login);

            User currentUser = userRepository.GetOrDie(login);

            if (currentUser.Pass == pass) userRepository.ChangePassword(login, newPass);
        }

        public void ChangeRole(string login, string pass, string roleUser, UserRole role)
        {
            Log.Info("ChangeRole: " + roleUser + " " + role);

            User currentUser = userRepository.GetOrDie(login);

            if(currentUser.Pass == pass)
            {
                userRepository.ChangeRole(roleUser, role);
            }
        }

        public void RemoveUser(string login, string pass, string userToRemove)
        {
            Log.Info("RemoveUser: " + userToRemove);

            User currentUser = userRepository.GetOrDie(login);

            if (currentUser.Pass == pass && currentUser.Role == (int)UserRole.Admin)
                userRepository.RemoveUser(userToRemove);
        }

        
    }

    [ServiceContract]
    public interface IUserWcfService
    {
        [OperationContract]
        User ValidateUser(string login, string pass);

        [OperationContract]
        User AddUser(string login, string pass, User newUser);

        [OperationContract]
        void ChangePassword(string login, string pass, string newPass);

        [OperationContract]
        void ChangeRole(string user, string pass, string roleUser, UserRole role);

        [OperationContract]
        void RemoveUser(string login, string pass, string userToRemove);
    }
}
